package transaction.imp;

import affilication.Affiliation;
import affilication.imp.UnionAffiliation;
import dao.PayrollDatabase;
import dao.imp.GpayrollDatabase;
import domain.Employee;
import domain.ServiceCharge;
import exception.EmployeeException;
import transaction.Transaction;

import java.util.Date;

public class ServiceChargeTransaction implements Transaction {
    private PayrollDatabase payrollDatabase = new GpayrollDatabase();
    private ServiceCharge serviceCharge;
    private Date date;
    public ServiceChargeTransaction(Date date, int memberId, double amount) {
        serviceCharge = new ServiceCharge(memberId, amount, date);
        this.date = date;
    }

    @Override
    public void execute() throws EmployeeException {
        Employee employee = payrollDatabase.getEmployeeByMemberId(serviceCharge.getMemberId());

        if(employee == null) {
            throw new EmployeeException("no such employee, memberId" + serviceCharge.getMemberId());
        }

        Affiliation affiliation = employee.getAffiliation();
        if(affiliation instanceof UnionAffiliation) {
            UnionAffiliation unionAffiliation = (UnionAffiliation) affiliation;
            unionAffiliation.addServiceCharge(serviceCharge);
        } else {
            throw new EmployeeException("the employee is not union affilication. empId=" + employee.getId());
        }
    }
}
